package com.vdong.common.oauth.service.impl;


import com.vdong.common.oauth.common.exception.WebBizException;
import com.vdong.common.oauth.entity.mo.WechatConfig;
import com.vdong.common.oauth.service.WechatConfigService;
import com.vdong.common.oauth.service.WechatService;
import com.vdong.common.oauth.repository.dao.WechatConfigMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class WechatConfigServiceImpl implements WechatConfigService {

    @Resource
    private WechatConfigMapper wechatConfigMapper;
    @Resource
    private WechatService wechatService;

    @Override
    public WechatConfig findByAppId(String appId) {
        return wechatConfigMapper.queryByAppId(appId);
    }

    @Override
    public WechatConfig getWechatConfigInfo(Long storeId) {
        return wechatConfigMapper.quertByStoreId(storeId);
    }

    @Override
    public Boolean updateProgram(WechatConfig wechatConfig, Long storeId) {
        WechatConfig config = this.getWechatConfigInfo(storeId);
        if (config == null) {
            wechatConfig.setStoreId(storeId);
            wechatConfig.setCreateDate(new Date());
            return wechatConfigMapper.insert(wechatConfig) > 0;
        } else {
            //修改小程序配置，清空缓存
//            wechatService.clearAuthorizerAccessToken(wechatConfig.getAppId());
            wechatConfig.setId(config.getId());
            wechatConfig.setModifyDate(new Date());
            return wechatConfigMapper.updateByPrimaryKey(wechatConfig) > 0;
        }
    }

    @Override
    public Boolean updateAppName(String appName, Long storeId) {
        WechatConfig config = this.getWechatConfigInfo(storeId);
        if (config == null) {
            config = WechatConfig.builder().storeId(storeId).appName(appName).createDate(new Date()).build();
//            config = new WechatConfig();
//            config.setAppName(appName);
//            config.setStoreId(storeId);
//            config.setCreateDate(new Date());
            return wechatConfigMapper.insert(config) > 0;
        } else {
            config.setAppName(appName);
            config.setModifyDate(new Date());
            return wechatConfigMapper.updateByPrimaryKey(config) > 0;
        }
    }

    @Override
    public Boolean updatePayCert(Long storeId, String fileName, byte[] file) {
        if (file == null) {
            throw new WebBizException("证书不能为空");
        }
        WechatConfig config = getWechatConfigInfo(storeId);
        if (config == null) {
            throw new WebBizException("请先保存小程序配置");
        }
        wechatConfigMapper.updatePayCert(config.getId(), file, fileName);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateH5Config(Long storeId, String h5AppId, String h5Secret) {
        WechatConfig config = getWechatConfigInfo(storeId);
        if (config == null) {
            throw new WebBizException("请先保存小程序配置");
        }
        config.setH5AppId(h5AppId);
        config.setH5Secret(h5Secret);
        return wechatConfigMapper.updateByPrimaryKey(config) > 0;
    }
}
